package main

import (
	"fmt"
	_ "gitee.com/edgebox/metadata/boot"
	. "gitee.com/edgebox/metadata/internal/log"
	. "gitee.com/edgebox/metadata/internal/routers"
	. "gitee.com/edgebox/pkg/log"
	"github.com/gin-gonic/gin"
	"runtime"
	"time"
)

func initrouter() *gin.Engine {
	gin.SetMode(gin.ReleaseMode)
	router := gin.Default()
	router.Use(loggingMiddleware())
	router.GET("/api/v1.0/metadata/app", GetApps)
	router.GET("/api/v1.0/metadata/global", GetKeyPairs)
	router.GET("/api/v1.0/metadata/global/:id", GetKeyPairById)
	router.GET("/api/v1.0/metadata/net", GetNets)
	router.GET("/api/v1.0/metadata/group", GetGroups)
	router.GET("/api/v1.0/metadata/item", GetItems)
	router.GET("/api/v1.0/metadata/item/:tag", GetItemsByTag)
	router.GET("/api/v1.0/metadata/:id/item", GetItemsById)
	router.GET("/api/v1.0/metadata/export", GetExports)
	router.GET("/api/v1.0/metadata/frp", GetFrps)
	router.GET("/api/v1.0/metadata/device", GetDevices)
	router.GET("/api/v1.0/metadata/app/status/:id", GetAppsStatusById)
	router.GET("/api/v1.0/metadata/app/status", GetAppsStatus)

	router.POST("/api/v1.0/metadata/items", AddItems)
	router.POST("/api/v1.0/metadata/item", AddItem)
	router.POST("/api/v1.0/metadata/group", AddGroup)
	router.POST("/api/v1.0/metadata/export", AddExport)
	router.POST("/api/v1.0/metadata/frp", AddFrp)
	router.POST("/api/v1.0/metadata/global", AddKeyPair)
	router.POST("/api/v1.0/metadata/app", AddApp)
	router.PUT("/api/v1.0/metadata/app", UpdateApp)
	router.PUT("/api/v1.0/metadata/export", UpdateExport)
	router.PUT("/api/v1.0/metadata/group", UpdateGroup)
	router.PUT("/api/v1.0/metadata/item", UpdateItem)
	router.PUT("/api/v1.0/metadata/app/status/:id", UpdateAppStatusById)
	router.PUT("/api/v1.0/metadata/global", UpdateKeyPair)
	router.PUT("/api/v1.0/metadata/device", SetDevices)
	router.PUT("/api/v1.0/metadata/net", UpdateNet)

	router.DELETE("/api/v1.0/metadata/group/:id", DelGroup)
	router.DELETE("/api/v1.0/metadata/item/:id/:code", DelItem)
	router.DELETE("/api/v1.0/metadata/items", DelItems)
	router.DELETE("/api/v1.0/metadata/export/:id", DelExport)
	router.DELETE("/api/v1.0/metadata/frp/:id", DelFrp)
	router.DELETE("/api/v1.0/metadata/app/:id", DelApp)
	router.DELETE("/api/v1.0/metadata/global/:id", DelKeyPair)

	router.GET("/", func(c *gin.Context) {
		routers := router.Routes()
		for _, v := range routers {
			fmt.Fprintln(c.Writer, v.Method, ":", v.Path)
		}
	})

	return router
}
func loggingMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		origin := c.Request.Header.Get("Origin") //请求头部
		c.Writer.Header().Set("Origin","test")
		fmt.Println("origin:",origin)
		c.Header("Access-Control-Allow-Origin", "*")  // 可将将 * 替换为指定的域名
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
		c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")
		start := time.Now()
		c.Next()
		Log.Info(c.Request.Method, c.Request.URL, time.Since(start))
	}
}
func main() {

	Log = NewLogger2("metadata")
	router := initrouter()
	sysType := runtime.GOOS
	if sysType == "linux" {
		// LINUX系统
		router.Run("0.0.0.0:28081")
	} else {
		router.Run("127.0.0.1:28081")
	}

}
